export default {
	interceptor: {
		request: null,
		response: null
	},
	request(options) {
		if (options) {
			return new Promise((resolve, reject) => {
				//请求拦截器处理
				if (this.interceptor.request) {
					this.interceptor.request(options)
				}
				options.complete = (response) => {
					//统一返回日志
					_reslog(response)
					let statusCode = response.statusCode;
					//返回拦截器处理
					if (this.interceptor.response) {
						this.interceptor.response(response);
					}
					if (statusCode === 200) { //成功
						resolve(response);
					} else {
						reject(response)
					}
				};

				// 统一的请求日志记录
				_reqlog(options);
				uni.request(options);
			});
		} else {
			console.log("http参数为空")
		}

	},
	get(options) {
		options.method = 'GET';
		return this.request(options)
	},
	post(options) {
		options.method = 'POST';
		return this.request(options)
	},
	put(options) {
		options.method = 'PUT';
		return this.request(options)
	},
	delete(options) {
		options.method = 'DELETE';
		return this.request(options)
	}
}

/**
 * 请求接口日志记录
 */
function _reqlog(req) {
	if (process.env.NODE_ENV === 'development') {
		console.log("请求参数：" + JSON.stringify(req))
	}
}

/**
 * 响应接口日志记录
 */
function _reslog(res) {
	if (process.env.NODE_ENV === 'development') {
		console.log("响应结果：" + JSON.stringify(res))
	}

}
